#include "Plane.h"
#include "math.h"

Plane::Plane() {}

Plane::Plane(Vector normal, float d)
{
    this->normal = normal;
    this->d = d;
}

//precondicion: near < far
bool Plane::intersect(const Ray &ray, float &near, float &far)
{
    float dist = (Vector(ray.p) * normal) + d;
    float dot = normal * ray.v;

    if (fabs(dot) < 0.0001) { //rayo paralelo al plano
        if (dist > 0.0)
            return false;

    } else {
        float lambda = -(dist/dot);

        if (dot < 0.0) { //plano front-face
            if (lambda > near)
                near = lambda;
        } else { //plano back-face
            if (lambda < far)
                far = lambda;
        }
    }

    return (near < far);
}
